TCP/IP协议笔记

TCP状态转换

reference

TCP选项

最大段大小(MSS)

选择确认选项(SACK)

窗口缩放选项

时间戳(TSOPT)与防回绕序列号(PAWS)

几种计时器

时间等待计时器

保活计时器

重传计时器

持久计时器

算法

慢启动

[TCP/IP详解 卷一(中文 第二版) P520]
The slow start algorithm is executed when a new TCP connection is created or when a loss has been detected due to a retransmission timeout (RTO).

A TCP begins in slow start by sending a certain number of segments (after the SYN exchange), called the initial window (IW). The value of IW was originally one SMSS, although with [RFC5681] it is allowed to be larger. The formula works as follows:

IW = 2*(SMSS) and not more than 2 segments (if SMSS > 2190 bytes) 
IW = 3*(SMSS) and not more than 3 segments (if 2190 ≥ SMSS > 1095 bytes) 
IW = 4*(SMSS) and not more than 4 segments (otherwise)

slow start operates by incrementing cwnd by min(N, SMSS) for each good ACK received, where N is the number of previously unacknowledged bytes ACKed by the received “good ACK.” A good ACK is one that returns a higher ACK number than has been seen so far.

拥塞避免

[TCP/IP详解 卷一(中文 第二版) P521]
cwnd is usually updated as follows for each received nonduplicate ACK:

SMSS = min(MTU - (head size of ip and tcp), MSS);
cwnd(n-1) = k * SMSS

cwnd(n) = (k + (1/k))*SMSS = cwnd(n-1) + (1/k)*SMSS

快速重传与恢复

一个RTT时间只能重传一个segment

Reno算法(标准TCP)

1.ssthresh is updated to no more than the value given in equation:

ssthresh = max(flight size / 2, 2 * SMSS)

2.The fast retransmit algorithm is performed, and cwnd is set to (ssthresh + 3*SMSS).
3.cwnd is temporarily increased by SMSS for each duplicate ACK received.
4.When a good ACK is received, cwnd is reset back to ssthresh.

newReno算法

This procedure modifies fast recovery by keeping track of the highest sequence number from the last transmitted window of data (the recovery point, which we first saw in Chapter 14). Only when an ACK with an ACK number at least as large as the recovery point is received is the inflation of fast recovery removed. This allows a TCP to continue sending one segment for each ACK it receives while recovering and reduces the occurrence of retransmission timeouts, especially when multiple packets are dropped in a single window of data.

超时重传

SACK

reference
The benefits of SACKs are more pronounced when the RTT is large and packet loss is severe. Under such circumstances, the benefits of being able to fill more than one hole per RTT are likely to be more significant.

FACK

DSACK

CORK算法

Nagle算法

[TCP/IP详解 卷一(中文 第二版) P495]
(1)如果包长度达到MSS,则允许发送;
(2)如果该包含有FIN,则允许发送;
(3)设置了TCP_NODELAY选项,则允许发送;
(4)未设置TCP_CORK选项时,若所有发出去的小数据包(包长度小于MSS)均被确认,则允许发送;
(5)上述条件都未满足,但发生了超时(一般为200ms),则立即发送。

Karn算法

[TCP/IP详解 卷一(中文 第二版) P467]
重传二义性
Karn算法的基本步骤:

  1. when a timeout and retransmission occur, we cannot update the RTT estimators when the acknowledgment for the retransmitted data finally arrives. This is the “first part” of Karn’s algorithm.
  2. TCP applies a backoff factor to the RTO, which doubles each time a subsequent retransmission timer expires. Doubling continues until an acknowledgment is received for a segment that was not retransmitted. At that time, the backoff factor is set back to 1 (i.e., the binary exponential backoff is canceled), and the retransmission timer returns to its normal value. Doubling the backoff factor on subsequent retransmissions is the “second part” of Karn’s algorithm.

API

#include <sys/socket.h>
int socket(int family, int type, int protocal);
#include <sys/socket.h>
int connect(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

connect激发三次握手,出错返回的错误码:
ETIMEDOUT
ECONNREFUSED
EHOSTUNREACH/ENETUNREACH

出错返回时候,若要重新连接,需要close套接字,然后重新调用socket创建新的套接字

int bind(int sockfd, const struct sockaddr *servaddr, socklen_t addrlen);

错误码:EADDRINUSE

my_sockaddress.sin_addr.s_addr=htonl(INADDR_ANY)

INADDR_ANY: binds the socket to all available interfaces.

The difference between 127.0.0.1 and 0.0.0.0 ?

int listen(int sockfd, int backlog);

The behavior of the backlog argument on TCP sockets changed with Linux 2.2. Now it specifies the queue length for completely established sockets waiting to be accepted, instead of the number of incomplete connection requests. The maximum length of the queue for incomplete sockets can be set using /proc/sys/net/ipv4/tcp_max_syn_backlog. When syncookies are enabled there is no logical maximum length and this setting is ignored. See tcp(7) for more information.

If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it is silently truncated to that value; the default value in this file is 128. In kernels before 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value 128.

拓展:
SYN BLOOD
Introdution
Solution 1 : 增加SYN队列的长度或缩短SYN超时时间 (/proc/sys/net/ipv4)
Solution 2 : SYN cookies 1
Solution 2 : SYN cookies 2
Solution 2 : SYN cookies 3

int accept(int sockfd, struct sockaddr* cliaddr, socklen_t* addrlen); 
int close(int sockfd); 
//close将描述符引用计数减一,当为0时,关闭套接字

int shutdown(int sockfd, int howto); 
// 不关心引用计数,且可以实现半关闭。
// howto: SHUT_RD, SHUT_WR, SHUT_RDWR 

其他协议

ARP

ICMP(ping, traceroute)


shiyang6017
158 声望59 粉丝

引用和评论

0 条评论